<template>
  <div class="content-workbench">
    <div class="inner-container">
      <div class="top-box">
        <div class="inner-container-header" v-loading="stationStatisticsLoading">
          <div class="toDo">
            <h1>
              <i class="iconfont title-icon">&#xe6e3;</i>
              我的内容
              <div class="more-toDo" @click="goRoute('/content/my/release', {state: 99})">
                详情
                <i class="iconfont details-icon">&#xe601;</i>
              </div>
              <!-- <div class="more-toDo" @click="goRoute('/content/my/release')">详情<i class="iconfont details-icon">&#xe601;</i></div> -->
            </h1>
            <div class="myManuscript-box">
              <div class="detail-box title-box allNum center-detail-box">
                <span class="label">我的稿件</span>
                <span class="num blue">{{ stationStatisticsInfo.pass ? stationStatisticsInfo.pass : 0 + stationStatisticsInfo.draft ? stationStatisticsInfo.draft : 0 + stationStatisticsInfo.revoke ? stationStatisticsInfo.revoke : 0 }}</span>
              </div>
              <div class="detail-box">
                <div class="row">
                  <div class="detail" @click="goRoute('/content/my/release', {state: 99})">
                    <span class="label">已发布：</span>
                    <span class="value" :class="stationStatisticsInfo.pass < 1 ? 'no-num' : ''">{{ stationStatisticsInfo.pass }}</span>
                  </div>
                  <div class="detail" @click="goRoute('/content/my/release', {state: 1})">
                    <span class="label">草稿箱：</span>
                    <span class="value" :class="stationStatisticsInfo.draft < 1 ? 'no-num' : ''">{{ stationStatisticsInfo.draft || 0 }}</span>
                  </div>
                  <div class="detail" @click="goRoute('/content/my/release', {state: 2})">
                    <span class="label">撤稿箱：</span>
                    <span class="value" :class="stationStatisticsInfo.revoke < 1 ? 'no-num' : ''">{{ stationStatisticsInfo.revoke }}</span>
                  </div>
                </div>
              </div>
              <div class="detail-box">
                <div class="row">
                  <div class="detail" @click="goRoute('/content/my/collect', {state: 99})">
                    <span class="label">我的收藏：</span>
                    <span class="value">{{ stationStatisticsInfo.collects }}</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="toDo">
            <h1>
              <i class="iconfont title-icon">&#xe6db;</i>
              审核
              <div class="more-toDo" @click="goFlow('/flow/task/pending', {moduleId: 1})">
                详情
                <i class="iconfont details-icon">&#xe601;</i>
              </div>
            </h1>
            <div class="myManuscript-box">
              <div class="detail-box title-box allNum" @click="goFlow('/flow/task/started', {moduleId: 1})">
                <span class="label">我提交的审核</span>
                <span class="num blue" :class="stationStatisticsInfo.myFlowing < 1 ? 'no-num' : ''">{{ stationStatisticsInfo.myFlowing }}</span>
              </div>
              <div class="detail-box title-box allNum" @click="goFlow('/flow/task/pending', {moduleId: 1})">
                <span class="label">待我审核</span>
                <span class="num" :class="stationStatisticsInfo.myAudit < 1 ? 'no-num' : ''">{{ stationStatisticsInfo.myAudit }}</span>
              </div>
              <div class="detail-box title-box allNum" @click="goRoute('/content/my/release', {state: 3})">
                <span class="label">待处理退稿</span>
                <span class="num" :class="stationStatisticsInfo.myReject < 1 ? 'no-num' : ''">{{ stationStatisticsInfo.myReject }}</span>
              </div>
            </div>
          </div>
        </div>
        <div class="release">
          <div class="release-header">
            <span class="release-title">快速发布</span>
            <span class="release-more" @click="linkMore">更多发布</span>
          </div>
          <ul v-loading="navListLoading">
            <template v-for="(item, index) in navList" :key="index">
              <li v-if="item.showModel">
                <a href="javascript:;" @click="linkTo(item)">
                  <div class="icon-box">
                    <i :class="item.icon + ' iconfont'" :style="{ color: themeColor }"></i>
                  </div>
                  <p>{{ item.name }}</p>
                </a>
              </li>
            </template>
            <template v-for="(item, index) in authoringToolList" :key="item.name">
              <li :class="index == 0 ? 'authoring-tool-li' : ''" v-if="item.showModel">
                <a href="javascript:;" @click="openTo(item)">
                  <div class="icon-box">
                    <i class="iconfont" :style="{ color: item.color }" v-html="item.icon"></i>
                  </div>
                  <p>{{ item.name }}</p>
                </a>
              </li>
            </template>
          </ul>
        </div>
      </div>
      <div class="inner-container-buttom">
        <!-- 常用栏目 -->
        <!-- <commonCategory/> -->
        <!-- 稿件列表 -->
        <!-- <ContentList /> -->
      </div>
    </div>
    <DialogTreeJump
      v-if="showChooseCategory"
      v-model:showDialog="showChooseCategory"
      :pushCategoryList="categoryList"
      :pushCategoryCommonList="commonCategoryList"
      :isSingle="true"
      :curModel="curModel"
      @treeChoosed="goRelease"
    ></DialogTreeJump>
    <!-- 快速发布 更多 -->
    <el-dialog v-model="quickPublishMoreOpen" class="quick-publish-dialog" title="快速发布" width="30%">
      <template #header>
        <el-tabs v-model="quickPublishActive" class="quick-publish-dialog-tabs">
          <el-tab-pane label="发布模型" :name="0"></el-tab-pane>
          <!-- <el-tab-pane label="创作工具" :name="1"></el-tab-pane> -->
        </el-tabs>
      </template>
      <div class="des">
        <div class="left">类型</div>
        <div class="right">显示</div>
      </div>
      <!-- 发布模型 -->
      <ul class="more-publish" v-if="quickPublishActive === 0">
        <li v-for="(item, index) in navList" :key="index">
          <a href="javascript:;" @click="linkTo(item)">
            <Icon :name="item.icon" />
            <p>{{ '发布' + item.name }}</p>
          </a>
          <el-switch
            v-model="item.showModel"
            :active-value="1"
            :inactive-value="0"
            @change="
              val => {
                return handleModelShowChange(item)
              }
            "
          />
        </li>
      </ul>
      <!-- 创作工具 -->
      <ul class="more-publish" v-if="quickPublishActive === 1">
        <li v-for="(item, index) in authoringToolList" :key="index">
          <a href="javascript:;" @click="openTo(item)">
            <i class="iconfont authoring-tool" :style="{ color: item.color }" v-html="item.icon"></i>
            <p>{{ item.name }}</p>
          </a>
          <el-switch v-model="item.showModel" :active-value="1" :inactive-value="0" @change="handleModelShowChange" />
        </li>
      </ul>
    </el-dialog>
  </div>
</template>

<script setup>
/* eslint-disable */
import { reactive, ref, nextTick, computed } from 'vue'
import { getOfflineList, getRejectionList, getWaitingAuditList, stationStatistics } from '@/api/home'
import { getModelList } from '@/api/model'
import { getCategoryByModelId } from '@/api/category'
import { getContentDashboard } from '@/api/content'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { setLocCanReleaseModel, getLocCanReleaseModel } from '@/utils/storage'
import { getToken } from '@/utils/storage'
import { jumpRoute, hasPermission } from '@ruifox-cms/utils'
import { ElMessage } from 'element-plus'

const store = useStore()
const router = useRouter()
const data = reactive({
  waitingAuditTotal: null,
  rejectionTotal: null,
  offlineTotal: null
})
const siteId = computed(() => store.state.site.currentSite?.id)
const themeColor = store.state.base.themeColor

// //获取待审核的数量
// getWaitingAuditList().then(res => {
//   if(res.code == 20000) {
//     data.waitingAuditTotal = res.data.total
//   }
// })
// // 待处理退稿数量
// getRejectionList().then(res => {
//   if(res.code == 20000) {
//     data.rejectionTotal = res.data.total
//   }
// })
// // 即将下线
// getOfflineList().then(res => {
//   if(res.code == 20000) {
//     data.offlineTotal = res.data.total
//   }
// })
const stationStatisticsInfo = ref({})
const stationStatisticsLoading = ref(false)
// 获取工作台顶部统计（我的内容+审核）
const handleGetStationStatistics = async () => {
  stationStatisticsLoading.value = true
  let res = await getContentDashboard({ siteId: siteId.value })
  stationStatisticsInfo.value = res.data
  stationStatisticsLoading.value = false
}
handleGetStationStatistics()

// 跳转待审详情(moduleId: 1标识站群模块的审核流)
const goFlow = (path, query) => {
  jumpRoute(path, query, '_blank')
}

// 创作工具
const authoringToolList = ref([
  // { id: 1, name: '在线设计', icon: '&#xe67f;', link: '', showModel: 1, color: '#E1943A' }
  // { id:2, name: '视频编辑', icon: '&#xe64a;', link: '', showModel: 1, color:'#F2709F' },
])
const handleModelShowChange = val => {
  if (navList.value.filter(item => item.showModel == 1).length < 1) {
    nextTick(() => {
      val.showModel = 1
      ElMessage({
        message: '至少保留一项',
        type: 'warning'
      })
    })
  }
  let list = navList.value.map(item => {
    return {
      id: item.id,
      showModel: item.showModel === undefined ? 1 : item.showModel
    }
  })
  // 本地缓存显隐状态
  setLocCanReleaseModel({
    navStatusList: list,
    authoringToolList: authoringToolList.value
  })
}
const navList = ref([])
const navStatusList = ref([]) //navList显示隐藏状态列表
const navListLoading = ref(true)
const handleGetCanReleaseModel = () => {
  let locData = getLocCanReleaseModel()
  navStatusList.value = locData?.navStatusList.length ? locData.navStatusList : []
  authoringToolList.value = locData?.authoringToolList.length ? locData.authoringToolList : authoringToolList.value
  // 获取可以发布模型列表
  getModelList({state: 0}).then(res => {
    // 获取本地显隐状态
    navList.value = res.data.map(item => {
      let curObj = navStatusList.value.find(tmp => tmp.id === item.id && item.state === 0)
      return {
        ...item,
        showModel: curObj === undefined ? 1 : curObj.showModel
      }
    })
    navListLoading.value = false
    handleModelShowChange()
  })
}
handleGetCanReleaseModel()
const quickPublishActive = ref(0)

const curModel = ref(null)
const pushCategory = ref('')
const showChooseCategory = ref(false)
const goModel = reactive({})
const categoryList = ref([])
const commonCategoryList = ref([]) // java后台暂无最近常用栏目的接口
const linkTo = item => {
  if(!hasPermission(['content:workStation:publish'])) {
    ElMessage.warning('您没有发布权限，请联系管理员添加')
    return
  }
  curModel.value = item
  showChooseCategory.value = true
  pushCategory.value = ''
  Object.assign(goModel, item)
  getCategoryByModelId({ modelId: item.id, siteId: siteId.value }).then(res => {
    categoryList.value = res.data
  })
}
const openTo = data => {
  let token = getToken()
  if (data.id == 1) {
    //在线设计
    if (process.env.NODE_ENV === 'development') {
      window.open(`http://localhost:3000/#/index?token=${token}`, '_blank')
    } else {
      window.open(`http://banner.foxtest.net/#/index?token=${token}`, '_blank')
    }
  } else if (data.id == 2) {
    window.open(`https://www.llbbfly.top/preview?token=${token}`, '_blank')
  }
}
// 去发布
const goRelease = val => {
  console.log(val, 89898989)
  // showChooseCategory.value = false
  // window.open(toUrl.href,"_blank")
}
const quickPublishMoreOpen = ref(false)
const linkMore = () => {
  quickPublishMoreOpen.value = true
}
const goRoute = (path, query) => {
  if (path) router.push({ path, query })
}
</script>

<style scoped lang="scss">
.content-workbench {
  flex: 1;
  .inner-container {
    width: 100%;
    .top-box {
      background: #fff;
      padding: 20px;
      padding-bottom: 0;
    }
    .inner-container-header {
      display: flex;
      flex-wrap: wrap;
    }
    .inner-container-buttom {
      display: flex;
      justify-content: center;
      align-items: flex-start;
      .my-content {
        // width: calc(100% - 210px);
        flex: 1;
      }
    }
    .drag-class {
      opacity: 1;
    }
    .drag-container {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
    }
    .toDo {
      width: calc((100% - 60px) / 3);
      max-width: 530px;
      min-width: 400px;
      height: 180px;
      background-color: #fff;
      border-radius: 8px;
      box-shadow: 0 0 5px rgba(#000, 0.1);
      box-sizing: border-box;
      padding: 20px 20px;
      margin-right: 20px;
      margin-bottom: 10px;
      // flex: 1;
      display: flex;
      flex-direction: column;
      h1 {
        color: #333;
        font-size: 18px;
        margin-bottom: 20px;
        display: flex;
        justify-content: flex-start;
        align-items: center;
        position: relative;
        .more-toDo {
          position: absolute;
          top: 50%;
          right: 0;
          transform: translateY(-50%);
          font-size: 14px;
          color: #aaa;
          font-weight: normal;
          cursor: pointer;
          .details-icon {
            transform: rotate(-90deg);
            font-size: 14px;
          }
        }
        .title-icon {
          font-size: 20px;
          margin-right: 10px;
        }
      }
  
      .myManuscript-box {
        width: 100%;
        height: 90px;
        display: flex;
        justify-content: center;
        align-items: center;
  
        .allNum {
          span {
            white-space: nowrap;
            font-size: 16px;
            color: #797979;
          }
          .num {
            font-size: 32px;
            color: #333;
            margin-top: 20px;
          }
          .blue {
            color: $mainColor;
          }
          &:hover {
            .label {
              @include textHover;
            }
          }
        }
        .detail-box {
          width: 130px;
          height: 100%;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          align-items: center;
          // width: 30%;
          // height: 100%;
          margin: 0 10px;
          .row {
            display: flex;
            justify-content: space-between;
            flex-direction: column;
            align-items: center;
            height: 100%;
            width: 100%;
            .detail {
              color: #555;
              font-size: 16px;
              display: flex;
              justify-content: flex-start;
              align-items: center;
              flex-wrap: nowrap;
              width: 100%;
              cursor: pointer;
              .label {
                color: #797979;
              }
              .value {
                color: #333;
              }
              span {
                white-space: nowrap;
              }
              &:hover {
                span {
                  @include textHover;
                }
              }
            }
          }
        }
        .title-box {
          justify-content: center;
          align-items: flex-start;
          cursor: pointer;
          .first-text {
            margin-top: 6px;
            font-weight: 600;
          }
          .iconfont {
            transition: all 0.2s;
            font-size: 40px;
            color: $mainColor;
          }
        }
        .center-detail-box {
          position: relative;
          &::before {
            content: '';
            position: absolute;
            right: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 1px;
            height: 100%;
            background: #d7d7d7;
          }
        }
      }
    }
  
    .release {
      flex: 1;
      margin-right: 0;
      .release-header{
        display: flex;
        justify-content: space-between;
        align-items: end;
        padding: 20px 0;
        border-bottom: 1px #d7d7d7 solid;
        .release-title {
          font-size: 18px;
          font-weight: 600;
        }
        .release-more {
          font-size: 14px;
          color: #7f7f7f;
          font-weight: normal;
          cursor: pointer;
        }
      }
      ul {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        flex-wrap: wrap;
        width: 100%;
        padding: 20px 0;
        li {
          height: 100%;
          padding: 0 40px;
          margin: 20px 0;
          &:hover {
            a {
              color: $mainColor;
              .icon-box {
                transform: scale(1.1);
              }
            }
          }
          a {
            height: 100%;
            width: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: space-around;
            .icon-box {
              width: 50px;
              height: 50px;
              border-radius: 10px;
              display: flex;
              justify-content: center;
              align-items: center;
              background: $checkColor;
              transition: all 0.2s;
            }
            i {
              font-size: 30px;
            }
            p {
              transition: all 0.2s;
              margin-top: 14px;
              color: #333;
              font-size: 14px;
            }
          }
        }
        .authoring-tool-li {
          border-left: 1px #d7d7d7 solid;
        }
      }
    }
    .content-item {
      background-color: #fff;
      border-radius: 8px;
      box-shadow: 0 0 5px rgba(#000, 0.1);
      width: calc(50% - 7px);
      margin-top: 15px;
      box-sizing: border-box;
      padding: 20px;
      min-width: 650px;
      h1.left-tit-more {
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 60px;
        a.tit {
          display: flex;
          align-items: center;
          span {
            font-size: 18px;
            line-height: 1;
          }
          i {
            color: $mainColor;
            font-size: 32px;
            /* margin-right: 10px; */
            font-weight: normal;
            line-height: 18px;
            &:hover {
              cursor: move;
            }
          }
        }
        a.more {
          font-size: 14px;
          line-height: 1;
          color: #7f7f7f;
          font-weight: normal;
        }
      }
      table {
        width: 100%;
        thead tr th {
          font-size: 12px;
          line-height: 20px;
          color: #aaaaaa;
          text-align: left;
          font-weight: normal;
        }
        tbody tr td {
          color: #555;
          height: 48px;
          border-bottom: 1px solid #e4e4e4;
          font-size: 14px;
          /* width: 140px; */
          overflow: hidden;
          white-space: nowrap;
        }
        tbody tr {
          td.rank span {
            display: inline-block;
            width: 24px;
            height: 24px;
            line-height: 24px;
            text-align: center;
            border-radius: 4px;
            font-size: 14px;
            color: #fff;
            background-color: #ff9700;
          }
          &:nth-child(2) {
            td.rank span {
              background-color: #2cbf77;
            }
          }
          &:nth-child(3) {
            td.rank span {
              background-color: #4d99f2;
            }
          }
          &:nth-child(4) {
            td.rank span {
              background-color: #73b2fb;
            }
          }
          &:nth-child(5) {
            td.rank span {
              background-color: #aad1ff;
            }
          }
        }
      }
      .top-tool {
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 50px;
        .btn-box {
          display: flex;
          border-radius: 4px;
          overflow: hidden;
          .btn {
            padding: 0 17px;
            font-size: 14px;
            line-height: 30px;
            background-color: rgba(#000, 0.04);
            color: #7f7f7f;
            border: 1px solid #dcdfe6;
            cursor: pointer;
            & + .btn {
              border-left: none;
            }
            &.active {
              border: none;
              background-color: $mainColor;
              color: #fff;
            }
          }
        }
      }
    }
  }
  ::v-deep(.quick-publish-dialog) {
    .el-dialog__header {
      border-bottom: none;
    }
    .el-dialog__body {
      padding-top: 0;
    }
    .des {
      width: 100%;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 15px;
      padding-bottom: 5px;
      color: #999;
    }
  }
  .more-publish {
    padding-left: 0;
    li {
      font-size: 16px;
      padding: 10px 10px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      cursor: pointer;
      .el-button {
        display: none;
      }
      &:hover {
        background: #eeecec;
        p {
          text-decoration: underline;
          color: $mainColor;
        }
        .el-button {
          display: block;
        }
      }
      a {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        p {
          margin-left: 8px;
        }
        i {
          font-size: 20px;
        }
        .authoring-tool {
          font-size: 16px;
        }
      }
    }
  }
  .no-num {
    color: #797979 !important;
  }
}
</style>
